<?php

namespace App\Jobs;

use App\Models\cms\Course;
use Illuminate\Bus\Queueable;
use Illuminate\Queue\SerializesModels;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;

class UpdateCourseByUrl implements ShouldQueue
{
    use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;

    protected $id;
    protected $classId;
    // public $tries = 5;
    public $timeout = 360;

    /**
     * Create a new job instance.
     *
     * @return void
     */
    public function __construct($id, $classId)
    {
        $this->id = $id;
        $this->classId = $classId;
    }

    /**
     * Execute the job.
     *
     * @return void
     */
    public function handle()
    {
        //请求腾讯云视频点播后台
        /*if ($this->attempts() > 3) {
            echo "任务失败次数过多";
        }
        $data =  Course::pull($this->classId);
        $len =count($data);
        $name = Course::queryCourse(['id' => $this->id],['course_title']);
        $name = $name[0]['course_title'];
        if ($data)
        {
            foreach ($data as $k => $v)
            {
                    $id = json_decode($v['eventContent']['data']['fileName'],true)['id'];
                    if ($id)
                    {
                        Course::updateCourseById(['id' => $id],['url' => $v['eventContent']['data']['playSet'][1]['url'],'hd_url' => $v['eventContent']['data']['playSet'][2]['url']]);
                        Course::confirm();   //入库完毕,消费掉这些事件
                    }
            }
        }*/

         $this->release(30);
    }

    public function failed()
    {
        echo "job is failed";
    }
}
